package com.gopay.cron.service.cloud.gen;

import com.gopay.cron.service.cloud.gen.bean.*;
import com.gopay.cron.web.vo.CloudAccountStatementVO;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Johnson on 2017/3/1.
 */
public class CloudOrderFileGenHelper {

    private static Logger logger = Logger.getLogger(CloudOrderFileGenHelper.class);

    private String filePath;
    private String prodCode;
    private String checkDate;
    private String acctId;  //add by songfengli 20170821

    private RecvFileInfo recvFileInfo = new RecvFileInfo();
    private RefundFileInfo refundFileInfo = new RefundFileInfo();
    private TransferFileInfo transferFileInfo = new TransferFileInfo();
    private PayFileInfo payFileInfo = new PayFileInfo();
    private WithdrawFileInfo withdrawFileInfo = new WithdrawFileInfo();

    public void addRecvFileLine(CloudAccountStatementVO vo) {
        recvFileInfo.addRecvFileLine(vo);
    }

    public void addRefundFileLine(CloudAccountStatementVO vo) {
        refundFileInfo.addRefundFileLine(vo);
    }

    public void addTransferFileLine(CloudAccountStatementVO vo) {
        transferFileInfo.addTransferFileLine(vo);
    }

    public void addPayFileLine(CloudAccountStatementVO vo) {
        payFileInfo.addPayFileLine(vo);
    }

    public void genFile() throws IOException {
        File path = new File(getFilePath());
        if(!path.exists()) {
            path.mkdirs();
        }

        if("CLOUD".equals(getProdCode()) || "GNST".equals(getProdCode())) {
            // 1.生成收款对账文件
            String recvFileName = getCheckDate() + "_" + RecvFileInfo.fileName;
            File recvFile = new File(getFilePath() + recvFileName + ".txt");
            writeFile(recvFile,recvFileInfo.getTxtContent());

            recvFile = new File(getFilePath() + recvFileName + ".csv");
            writeFile(recvFile,recvFileInfo.getCsvContent());

            //2.生成退款对账文件
            String refundFileName = getCheckDate() + "_" + RefundFileInfo.fileName;
            File refundFile = new File(getFilePath() + refundFileName + ".txt");
            writeFile(refundFile,refundFileInfo.getTxtContent());

            refundFile = new File(getFilePath() + refundFileName + ".csv");
            writeFile(refundFile,refundFileInfo.getCsvContent());

            //3.生成付款对账文件
            String payFileName = getCheckDate() + "_" + PayFileInfo.fileName;
            File payFile = new File(getFilePath() + payFileName + ".txt");
            writeFile(payFile,payFileInfo.getTxtContent());

            payFile = new File(getFilePath() + payFileName + ".csv");
            writeFile(payFile,payFileInfo.getCsvContent());
        }

        if("CLOUD".equals(getProdCode())) {
            //4.生成转账对账文件
            String transferFileName = getCheckDate() + "_" + TransferFileInfo.fileName;
            File transferFile = new File(getFilePath() + transferFileName + ".txt");
            writeFile(transferFile,transferFileInfo.getTxtContent());

            transferFile = new File(getFilePath() + transferFileName + ".csv");
            writeFile(transferFile,transferFileInfo.getCsvContent());
        }
    }

    private void writeFile(File file,String content) throws IOException {
        FileOutputStream fos = null;
        PrintWriter writer = null;
        try {
            fos = new FileOutputStream(file);
            writer = new PrintWriter(fos);
            writer.write(content);
            writer.flush();
        } finally {
            if(fos != null) {
                fos.close();
            }
            if(writer != null) {
                writer.close();
            }
        }
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getProdCode() {
        return prodCode;
    }

    public void setProdCode(String prodCode) {
        this.prodCode = prodCode;
    }

    public String getCheckDate() {
        return checkDate;
    }

    public void setCheckDate(String checkDate) {
        this.checkDate = checkDate;
    }

    public String getAcctId() {
        return acctId;
    }

    public void setAcctId(String acctId) {
        this.acctId = acctId;
    }

    public void genFtpFile() throws IOException {
        File path = new File(getFilePath());
        if(!path.exists()) {
            path.mkdirs();
        }

        if("GNST".equals(getProdCode())) {
            // 1.生成收款对账文件
            String recvFileName = getCheckDate() + "-" + getAcctId() + "-" + RecvFileInfo.fileFtpName;
            File recvFile = new File(getFilePath() + recvFileName + ".csv");
            writeFile(recvFile, recvFileInfo.getFtpCsvContent());

            //2.生成退款对账文件
            String refundFileName = getCheckDate() + "-" + getAcctId() + "-" + RefundFileInfo.fileFtpName;
            File refundFile = new File(getFilePath() + refundFileName + ".csv");
            writeFile(refundFile, refundFileInfo.getFtpCsvContent());

            //3.生成转账对账文件
            String transferFileName = getCheckDate() + "-" + getAcctId() + "-" + TransferFileInfo.fileFtpName;
            File transferFile = new File(getFilePath() + transferFileName + ".csv");
            writeFile(transferFile,transferFileInfo.getFtpCsvContent());
        }
        if("CLOUD".equals(getProdCode())) {

            //1.生成提现对账文件
            String withdrawFileName = getCheckDate() + "-" + getAcctId() + "-" + WithdrawFileInfo.fileFtpName;
            File withdrawFile = new File(getFilePath() + withdrawFileName + ".csv");
            writeFile(withdrawFile,withdrawFileInfo.getFtpCsvContent());
        }

//        if("CLOUD".equals(getProdCode())) {
//            //4.生成转账对账文件
//            String transferFileName = getCheckDate() + "-" + getAcctId() + "-" + TransferFileInfo.fileFtpName;
//            File transferFile = new File(getFilePath() + transferFileName + ".csv");
//            writeFile(transferFile,transferFileInfo.getFtpCsvContent());
//        }
    }

    //FTP收款类合计
    public void addFtpFirstRecvFileLine(CloudAccountStatementVO vo) {
        recvFileInfo.addFtpFirstRecvFileLine(vo);
    }

    //FTP收款类明细
    public void addFtpSecondRecvFileLine(CloudAccountStatementVO vo) {
        recvFileInfo.addFtpSecondRecvFileLine(vo);
    }

    //FTP收款退款类合计
    public void addFtpFirstRefundFileLine(CloudAccountStatementVO vo) {
        refundFileInfo.addFtpFirstRefundFileLine(vo);
    }

    //FTP收款退款类明细
    public void addFtpSecondRefundFileLine(CloudAccountStatementVO vo) {
        refundFileInfo.addFtpSecondRefundFileLine(vo);
    }

    //FTP转账类合计
    public void addFtpFirstTransferFileLine(CloudAccountStatementVO vo) {
        transferFileInfo.addFtpFirstTransferFileLine(vo);
    }

    //FTP转账类明细
    public void addFtpSecondTransferFileLine(CloudAccountStatementVO vo) {
        transferFileInfo.addFtpSecondTransferFileLine(vo);
    }

    //FTP提现类合计
    public void addFtpFirstWithdrawFileLine(CloudAccountStatementVO vo) {
        withdrawFileInfo.addFtpFirstWithdrawFileLine(vo);
    }

    //FTP提现类明细
    public void addFtpSecondWithdrawFileLine(CloudAccountStatementVO vo) {
        withdrawFileInfo.addFtpSecondWithdrawFileLine(vo);
    }

}
